﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Collections.Generic;

using OfficeOutlook = Microsoft.Office.Interop.Outlook;


namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    public class ContactInfo {
        private static Dictionary<string, string> _phoneTypes;

        static ContactInfo() {
            _phoneTypes = new Dictionary<string, string>();

            string[] pairs = Properties.Resources.CONTACT_NUMBER_TYPES.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string pair in pairs) {
                string[] values = pair.Split(new char[] { '=' },StringSplitOptions.None);
                _phoneTypes.Add(values[0], values[1]);
            }

        }

        public static bool IsPropertySupported(string name) {
            return _phoneTypes.ContainsKey(name);
        }

        private OfficeOutlook.ContactItem _contact;
        private List<PhoneNumber>         _numbers;
        private int                       _selected = 0;

        public ContactInfo(OfficeOutlook.ContactItem contact) {
            _contact = contact;
        }

        public OfficeOutlook.ContactItem ContactItem {
            get {
                return _contact;
            }
        }

        public int SelectedIndex {
            get {
                return _selected;
            }
            set {
                _selected = value;
            }
        }

        public PhoneNumber SelectedItem {
            get {
                return _numbers[_selected];
            }
        }

        public bool HasPhoneNumbers {
            get {
                return (PhoneNumbers.Count > 0);
            }
        }

        public List<PhoneNumber> PhoneNumbers {
            get {
                if (_numbers == null) {
                    _numbers = new List<PhoneNumber>();

                    Dictionary<string, string>.Enumerator entries = _phoneTypes.GetEnumerator();
                    while (entries.MoveNext()) {
                        string value = PropertyValue(entries.Current.Key);
                        if (!String.IsNullOrEmpty(value))
                            _numbers.Add(new ContactInfo.PhoneNumber(this, entries.Current.Key, entries.Current.Value, value));
                    }


                }
                return _numbers;
            }
        }


        private string PropertyValue(string name) {
            return (string)_contact.ItemProperties[name].Value;
        }


        public class PhoneNumber {
            private ContactInfo _ci;
            private string      _id;
            private string      _number;
            private string      _type;

            public PhoneNumber(ContactInfo ci, string id, string type, string number) {
                _ci     = ci;
                _id     = id;
                _type   = type;
                _number = number;
            }

            public string Id { get { return _id; } }
            public string Type { get { return _type; } }
            public string Number { get { return _number; } }
            public ContactInfo ContactInfo { get { return _ci; } }

            public override string ToString() {
                return String.Format(Properties.Resources.CONTACT_NUMBER_FORMAT, _type, _number);
            }

        }

    }
}
